package com.dlwx.parttimetalent.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.bean.LabelClassifyBean;
import com.dlwx.parttimetalent.inter.OnRecyclerViewItemClickListener;
import com.dlwx.parttimetalent.utils.ScreenUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2017/12/11.
 */

public class HomeClassifyAdapter extends RecyclerView.Adapter<HomeClassifyAdapter.ViewHolder> implements View.OnClickListener{
    public List<LabelClassifyBean.BodyBean> datas = null;
    private double width;
    private Context context;
    public HomeClassifyAdapter(Context context, List<LabelClassifyBean.BodyBean> datas) {
        this.datas = datas;
        width=ScreenUtils.getScreenWidth();
        this.context=context;
    }
    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.tv_label,viewGroup,false);
        view.getLayoutParams().width= (int) (width/3.5);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.mTextView.setText(datas.get(position).getClassname());
        Glide.with(context).load(datas.get(position).getLabelicon()).into(viewHolder.mImageView);
        viewHolder.itemView.setTag(datas.get(position));
    }
    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }
    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public CircleImageView mImageView;
        public ViewHolder(View view){
            super(view);
            mTextView = (TextView) view.findViewById(R.id.tv_name);
            mImageView = (CircleImageView) view.findViewById(R.id.image);
        }
    }

    private OnRecyclerViewItemClickListener<LabelClassifyBean.BodyBean> mOnItemClickListener = null;
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public void onClick(View v) {
        //注意这里使用getTag方法获取数据
        mOnItemClickListener.onItemClick(v,(LabelClassifyBean.BodyBean)v.getTag());
    }
    public void clear(){
        datas.clear();
        notifyDataSetChanged();
    };
}
